package com.tdkj.service.financial.impl;

import java.sql.Timestamp;

import org.springframework.stereotype.Component;

import com.tdkj.model.base.BaseEntity;
import com.tdkj.model.financial.BeanFimamcialAmountChangeRecord;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.BaseService;
import com.tdkj.service.ServiceException;
import com.tdkj.service.financial.IBeanFimamcialAmountChangeRecordService;

@Component
public class BeanFimamcialAmountChangeRecordServiceImpl extends BaseService
		implements IBeanFimamcialAmountChangeRecordService {

	@Override
	public void createIn(Long userId, Integer money, String unit, Long refId,
			Class<? extends BaseEntity> refClass, String description) throws ServiceException {
		BeanUser user = this.getBaseDao().findById(userId, BeanUser.class);
		
		if("金币".equals(unit)){
			int balance = user.getFinancialAccount().getGold() + money;
			user.getFinancialAccount().setGold(balance);
		}else if("银币".equals(unit)){
			int balance = user.getFinancialAccount().getSilver() + money;
			user.getFinancialAccount().setSilver(balance);
		}else if("矿币".equals(unit)){
			int balance = user.getFinancialAccount().getMine() + money;
			user.getFinancialAccount().setMine(balance);
		}else{
			throw new ServiceException("系统不 支持此币种");
		}
		BeanFimamcialAmountChangeRecord bean = new BeanFimamcialAmountChangeRecord();
		bean.setUser(user);
		bean.setMoney(money);
		bean.setUnit(unit);
		bean.setRefId(refId);
		bean.setRefClass(refClass.getName());
		bean.setDescription(description);
		bean.setDateTime(new Timestamp(System.currentTimeMillis()));
		bean.setIsIn(true);
		this.getBaseDao().save(bean);
		
	}

	@Override
	public void createOut(Long userId, Integer money, String unit, Long refId,
			Class<? extends BaseEntity> refClass, String description) throws ServiceException {
		BeanUser user = this.getBaseDao().findById(userId, BeanUser.class);
		
		if("金币".equals(unit)){
			if(user.getFinancialAccount().getGold() >= money){
				int balance = user.getFinancialAccount().getGold() - money;
				user.getFinancialAccount().setGold(balance);
			}else{
				throw new ServiceException("账户余额不足");
			}
		}else if("银币".equals(unit)){
			if(user.getFinancialAccount().getSilver() >= money){
				int balance = user.getFinancialAccount().getSilver() - money;
				user.getFinancialAccount().setSilver(balance);
			}else{
				throw new ServiceException("账户余额不足");
			}
		}else if("矿币".equals(unit)){
			if(user.getFinancialAccount().getMine() >= money){
				int balance = user.getFinancialAccount().getMine() - money;
				user.getFinancialAccount().setMine(balance);
			}else{
				throw new ServiceException("账户余额不足");
			}
		}else{
			throw new ServiceException("系统不 支持此币种");
		}
		BeanFimamcialAmountChangeRecord bean = new BeanFimamcialAmountChangeRecord();
		bean.setUser(user);
		bean.setMoney(money);
		bean.setUnit(unit);
		bean.setRefId(refId);
		bean.setRefClass(refClass.getName());
		bean.setDescription(description);
		bean.setDateTime(new Timestamp(System.currentTimeMillis()));
		bean.setIsIn(false);
		this.getBaseDao().save(bean);
		
	}

}
